<?php

class Admin_StatistikaPresenter extends BasePresenter
{	
	public function renderDefault()
	{
		$statistika = new StatistikaModel();
		$this->template->statistiky = $statistika->findAll();
	}
	
	public function renderNovy()
	{
		$form = $this['statistikaForm'];
	}
	
	public function renderUpravit($id = 0)
	{
		$form = $this['statistikaForm'];
		if (!$form->isSubmitted()) {
			$statistika = new StatistikaModel();
			$row = $statistika->findById($id)->fetch();
			$this->template->image = $row['ikona'];
			if (!$row) {
				throw new NBadRequestException('Záznam nebyl nalezen.');
			}
			$form->setDefaults($row);
		}
	}
	
	public function renderSmazat($id = 0)
	{
		$statistika = new StatistikaModel();
		$this->template->statistika = $statistika->findById($id)->fetch();
		if (!$this->template->statistika) {
			throw new NBadRequestException('Statistika nebyla nalezena');
		}
	}
	
	protected function createComponentSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'smazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function smazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$statistika = new StatistikaModel();
			$statistika->delete($this->getParam('id'));
			$this->flashMessage('Statistika byla odstraněna.');
		}

		$this->redirect('default');
	}
	
	protected function createComponentStatistikaForm()
	{
		$form = new NAppForm;
		$form->addText('nazev', 'Název statistiky:');
		$form->addFile('ikona', 'Ikona:');
		$form->addSubmit('save', 'Uložit')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět')->getControlPrototype()->class('cancel')->setValidationScope(NULL);
		$form->onSubmit[] = callback($this, 'statistikaFormSubmitted');

		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		$this->template->form = $form;
		return $form;
	}
public function statistikaFormSubmitted(NAppForm $form)
	{
		if ($form['save']->isSubmittedBy()) {
			$id = (int) $this->getParam('id');
		
			$image = $form['ikona']->value;
			$statistika = new StatistikaModel();
			if ($id > 0) {
				$nazev = $this->uploadImages($id, $image, 'update');
				if($nazev == ''){
					$image_name = $statistika->findById($id)->fetch();
					$nazev = $image_name['ikona'];
				}
				
				$values = array(
					'nazev'=>$form['nazev']->value,
					'ikona'=>$nazev
				);
				$statistika->update($id, $values);
				
				$this->flashMessage('Statistika byla upravena.');
			} else {

				$values = array(
					'nazev'=>$form['nazev']->value,
					'ikona'=>'noimage.jpg'
				);
				
				$statistika->insert($values);
				$id = dibi::getInsertId();
				$nazev = $this->uploadImages($id, $image);
				$statistika->update($id, array('ikona'=>$nazev));
				$this->flashMessage('Statistika byla vytvořena.');
			}
		}

		$this->redirect('default');
	}
	public function uploadImages($id, $image, $operation = 'insert'){
		if($image->isOk()){
			$mimeType = $_FILES['ikona']['type'];
			switch($mimeType){
				case 'image/jpeg': $type = ".jpg";
				break;
				case 'image/gif': $type = ".gif";
				break;
				case 'image/png': $type = ".png";
				break;
				case 'image/tif': $type = ".tif";
				break;
				case 'image/tiff': $type = ".tiff";
				break;
				case 'image/bmp': $type = ".bmp";
				break;
			}

			$uploaddir = WWW_DIR . '/images/ikona_stat/';
			$filesMain = scandir(WWW_DIR.'/images/ikona_stat');
			$name = $id;    
			if($operation == 'update'){
				$statistika = new StatistikaModel();
				$image_name = $statistika->findById($id)->fetch();
				@unlink('../images/ikona_stat/'.$image_name['ikona']);
			}
			$uploadfile = $uploaddir . $name . $type;
			$image = NImage::fromFile($_FILES['ikona']['tmp_name']);
			$image->resize(200, 200);
			$image->alphaBlending(false);
			$image->saveAlpha(true);
			$image->save($path = $uploadfile);
			chmod($uploadfile, 0777);
			return $name.$type;
		}
	}
	
	
	/* POZICE */
	
	public function renderPoziceDefault()
	{
		$statistika = new StatistikaModel();
		$pozice = new PoziceModel();
		$pozice_statistika_all = $statistika->pozice_statistiky_findAll()->fetchAll();
		foreach($pozice_statistika_all as $item){
			$tmp_statistika = $statistika->findById($item['statistiky_id'])->fetch();
			$tmp_pozice = $pozice->findById($item['pozice_id'])->fetch();
			$item['statistika_nazev'] = $tmp_statistika['nazev'];
			$item['pozice_nazev'] = $tmp_pozice['nazev'];
			
		}
		NDebug::fireLog($pozice_statistika_all);
		$this->template->pozice = $pozice_statistika_all;
	}
	public function renderPozicePridat()
	{
		$form = $this['poziceForm'];
	}
	
	public function renderPoziceSmazat($id = 0)
	{
		$pozice = new StatistikaModel();
		$this->template->pozice = $pozice->pozice_statistiky_findById($id)->fetch();
		if (!$this->template->pozice) {
			throw new NBadRequestException('Pozice nebyla nalezena');
		}
	}
	
	protected function createComponentPoziceSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'poziceSmazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function poziceSmazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$pozice = new StatistikaModel();
			$pozice->pozice_statistiky_delete($this->getParam('id'));
			$this->flashMessage('Pozice byla odstraněna.');
		}

		$this->redirect('poziceDefault');
	}
	
	protected function createComponentPoziceForm()
	{
		$form = new NAppForm;
		$pozice = new PoziceModel();
		$statistika = new StatistikaModel();
		$pozice_items = $pozice->findAll()->fetchPairs('id','nazev');
		$form->addSelect('pozice_id', 'Pozice:', $pozice_items)->skipFirst('Zvolte pozici');
		$statistika_items = $statistika->findAll()->fetchPairs('id','nazev');
		$form->addSelect('statistiky_id', 'Statistika:', $statistika_items)->skipFirst('Zvolte statistiku');
		$form->addSubmit('save', 'Uložit')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět')->getControlPrototype()->class('cancel')->setValidationScope(NULL);
		$form->onSubmit[] = callback($this, 'poziceFormSubmitted');

		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		$this->template->form = $form;
		return $form;
	}
	public function poziceFormSubmitted(NAppForm $form)
	{
		
		if ($form['save']->isSubmittedBy()) {
			$id = (int) $this->getParam('id');
			$pozice = new StatistikaModel();
			if ($id > 0) {
				$pozice->pozice_statistiky_update($id, $form->getValues());
				$this->flashMessage('Pozice byla upravena.');
			} else {
				$pozice->pozice_statistiky_insert($form->getValues());
				$this->flashMessage('Pozice byla přidělena.');
			}
		}

		$this->redirect('poziceDefault');
	}
	/* ZAPAS */
	
	public function renderZapasDefault()
	{
		$statistika = new StatistikaModel();
		$zapas = new ZapasModel();
		$lide = new LideModel();
		$tym = new TymModel();
		$soutez = new SoutezModel();
		$zapas_statistika_all = $statistika->zapas_statistiky_findAll()->fetchAll();
		foreach($zapas_statistika_all as $item){
			$tmp_statistika = $statistika->findById($item['statistiky_id'])->fetch();
			$item['statistiky_nazev'] = $tmp_statistika['nazev'];
			
			$tmp_tym_lide_id = $tym->tym_lide_findById($item['tym_lide_id'])->fetch();
			$tmp_lide = $lide->findById($tmp_tym_lide_id['lide_id'])->fetch();
			$item['clovek_jmeno'] = $tmp_lide['jmeno'].' '.$tmp_lide['prijmeni'];
			
			$tmp_zapas = $zapas->findById($item['zapas_id'])->fetch();
			$item['cislo_zapasu'] = $tmp_zapas['cislo_zapasu'];
			
			$tmp_tym = $tym->findById($tmp_tym_lide_id['tym_id'])->fetch();
			$item['tym_nazev'] = $tmp_tym['nazev'];
			
			$tmp_tym_soutez = $soutez->tym_soutez_findById($item['tym_soutez_id'])->fetch();
			$tmp_soutez = $soutez->findById($tmp_tym_soutez['soutez_id'])->fetch();
			$item['soutez_nazev'] = $tmp_soutez['nazev'];
			
		}
		$this->template->zapasy = $zapas_statistika_all;

	}
	
	public function renderZapasSmazat($id = 0)
	{
		$statistika = new StatistikaModel();
		$this->template->statistika = $statistika->zapas_statistiky_findById($id)->fetch();
		if (!$this->template->statistika) {
			throw new NBadRequestException('Statistika nebyla nalezena');
		}
	}
	
	protected function createComponentZapasSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'zapasSmazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function zapasSmazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$statistika = new StatistikaModel();
			$statistika->zapas_statistiky_delete($this->getParam('id'));
			$this->flashMessage('Statistika byla odstraněna.');
		}

		$this->redirect('zapasDefault');
	}
	
	protected function createComponentZapasStatForm($name)
	{
		$form = new NAppForm($this, $name);
		$soutez = new SoutezModel();
		$statistika = new StatistikaModel();
		$form->addText('hodnota', 'Hodnota:');
		$statistika_items = $statistika->findAll()->fetchPairs('id','nazev');
		$form->addSelect('statistiky_id', 'Statistika:', $statistika_items)->skipFirst('Zvolte statistiku');
		$soutez_items = $soutez->findAll()->fetchPairs('id','nazev');
		$form->addSelect("soutez_id", "Soutěž:", $soutez_items)->skipFirst('Zvolte soutěž');
		
		$form->addDependentSelectBox("zapas_kola_id", "Kolo:", $form["soutez_id"], callback($this, "getValuesZapasKola"))->skipFirst('Zvolte kolo');
		if($this->isAjax())
			$form["zapas_kola_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		
		$form->addDependentSelectBox("zapas_id", "Zápas:",$form["zapas_kola_id"], callback($this, "getValuesZapas"))->skipFirst('Zvolte zápas');
		if($this->isAjax())
			$form["zapas_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		
		$form->addDependentSelectBox("tym_soutez_id", "Tým:",$form["zapas_id"], callback($this, "getValuesTymSoutez"))->skipFirst('Zvolte tým');
		if($this->isAjax())
			$form["tym_soutez_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		
		$form->addDependentSelectBox("tym_lide_id", "Hráči:",$form["tym_soutez_id"], callback($this, "getValuesTymLide"))->skipFirst('Zvolte hráče');
		if($this->isAjax())
			$form["tym_lide_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		
		
		$form->addSubmit('save', 'Uložit')->getControlPrototype()->class('default');
		$form['save']->onClick[] = callback($this, "zapasStatFormSubmitted");		
		$form->addSubmit('cancel', 'Zpět')->setValidationScope(NULL);
		$form['cancel']->onClick[] = callback($this, "zapasStatFormSubmitted");
		
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		$this->template->form = $form;
		return $form;
	}
	public function zapasStatFormSubmitted($button)
	{	
		
		if(isset($button->form->httpData['save'])){
			$values = array(
				'hodnota'=>$button->form['hodnota']->value,
				'zapas_id'=>$button->form['zapas_id']->value, 
				'statistiky_id'=>$button->form['statistiky_id']->value, 
				'tym_soutez_id' => $button->form['tym_soutez_id']->value,
				'tym_lide_id' => $button->form['tym_lide_id']->value
			);
			
			$id = (int) $this->getParam('id');
			$statistika = new StatistikaModel();
			$statistika->zapas_statistiky_insert($values);
			$this->flashMessage('Statistika byla vytvořena.');
			
		}

		$this->redirect('zapasDefault');
	}
	
	function getValuesZapasKola($form) {
		$soutez_id = $form["soutez_id"]->getValue();
		return dibi::query("SELECT id, nazev FROM dbo.Zapas_Kola WHERE soutez_id=%i", $soutez_id)->fetchPairs("id", "nazev");
	}
	function getValuesTymSoutez($form) {
		$tym = new TymModel();
		$soutez_id = $form["soutez_id"]->getValue();
		$tmp_tym_soutez = dibi::query("SELECT [*] FROM dbo.Tym_Soutez WHERE soutez_id=%i", $soutez_id)->fetchAll();
		$tymy_assoc = array();
		foreach($tmp_tym_soutez as $item){
			$tmp_tym = $tym->findById($item['tym_id'])->fetch();
			$tymy_assoc[$item['id']] = $tmp_tym['nazev'];
		}
		return $tymy_assoc; 
	}
	function getValuesZapas($form) {
		$zapas_kola = $form["zapas_kola_id"]->getValue();
		return dibi::query("SELECT [*] FROM dbo.Zapas WHERE zapas_kola_id=%i", $zapas_kola)->fetchPairs("id", "cislo_zapasu");
	}	

	function getValuesTymLide($form) {
		
		$lide = new LideModel();
		$tym = new SoutezModel();
		$tym_id = $form["tym_soutez_id"]->getValue();
		$tmp_tym_soutez = $tym->tym_soutez_findById($tym_id)->fetch();
		
		$tmp_tym_lide = dibi::query("SELECT [*] FROM dbo.Tym_Lide WHERE tym_id=%i", $tmp_tym_soutez['tym_id'])->fetchAll();
		$lide_assoc = array();
		
		foreach($tmp_tym_lide as $item){$tmp_tym_soutez =
			
			$tmp_lide = $lide->findById($item['lide_id'])->fetch();
			
			$lide_assoc[$item['id']] = $tmp_lide['jmeno'].' '.$tmp_lide['prijmeni'];
		}
		
		return $lide_assoc;
	}	
}

